import store from '@/store/index.js'
const api = store.getters.getUrl;
const http = (params) => {
	return new Promise((resolve, reject) => {
		//  传入的参数中含有isNoNeedLoading不需要加载中的提示
		if (!params.data.isNoNeedLoading) {
			
			uni.showLoading({
				mask: true,
				title: '加载中...',
			})
		}
		let needToken = true;
		// token可传可不传的接口开始-------------------------
		// token可传可不传的接口
		const specialApiList = [
			'privileged_activity/activity/front_get_activity_detail',
			'activity/activity/contact'
		]
		console.log(params.url)
		console.log(specialApiList)
		if(!params.domain&&!specialApiList.includes(params.url)){//传的参数中没有域名的时候
			params.data['token'] = store.getters.getToken
		}else {
			needToken = false
		}
		// token可传可不传的接口结束-------------------------
		
		// 加上token,防止忘写
		if ((params.data['token'] == '' || params.data['token'] == null || typeof params.data['token'] == undefined) &&
			store.getters.getToken != '') {
			params.data['token'] = store.getters.getToken
		}
		
		// 不需要token的接口
		const whitelist = [
			// '/verify/verify_code/code',
			'/activity/activity/get_select',
			'/thk/customer_organization/get_city',
			'/store/index/infoStore',
			'/cms/page/get_homepage',
		]

		if (whitelist.includes(params.url)) {
			delete params.data.token
			needToken = false;
		}
		let netType = null
		// 获取网络状态
		uni.getNetworkType({
			success: (res) => {
				netType = res.networkType;
				
				if(netType === 'none') {
					reject()
					uni.hideLoading()
					uni.showToast({
						title:'网络未连接，请检查网络',
						icon:'none',
						duration:1500,
						mask:true
					})
					return false
				}
				// 没有token时则跳转到登录页面
				if (needToken && !params.data.token) {
					if (!params.data.isNoNeedLoading) {
						uni.hideLoading()
					}
				} else {
					let realApi
					if(params.domain) {
						realApi = params.domain
					}else {
						realApi = api
					}
					uni.request({
						// url: api + params.url,
						url: realApi + params.url,
						data: params.data,
						header: params.header || {
							"Content-Type": "application/x-www-form-urlencoded"
						}, // 默认请求头，可自行设置
						method: params.method || 'post', // 请求格式默认为post，需要其他格式可自行设置
						dataType: params.dataType, // 小程序默认为json，可自行设置
						responseType: params.responseType, // 默认为text，可自行设置
						success: (res) => {
							if (res.statusCode === 200) {
								resolve(res.data)
							} else {
									uni.showToast({
										icon: 'none',
										title: res.data.message
									})
							}
							// console.log("到这了",params.url)
							if (!params.data.isNoNeedLoading) {
								if(params.data.loading==1){
									console.log("到这了",params.data)
								}else{
									uni.hideLoading()
								}
								// uni.hideLoading()
							}
						},
						fail: (e) => {
							reject(e)
							if (!params.data.isNoNeedLoading) {
								uni.hideLoading()
							}
						}
					})
				}
			}
		});
	})
}
module.exports = {
	http: http
}
